package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepDetailParam;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepDetailParamService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentUpkeepDetailParamController}", description = "保养明细选项")
@RestController
@RequestMapping("/erp/equipment/upkeep/category/param/")
public class EquipmentUpkeepDetailParamController {

    @Autowired
    private EquipmentUpkeepDetailParamService equipmentUpkeepDetailParamService;


    @GetMapping("list")
    @Operation(summary = "保养明细选项列表", description = "获取保养明细选项列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepDetailParam>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepDetailParam> list = equipmentUpkeepDetailParamService.list(EquipmentUpkeepDetailParam.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "保养明细选项列表", description = "获取保养明细选项列表（分页）", method = "GET")
    public Result<Page<EquipmentUpkeepDetailParam>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EquipmentUpkeepDetailParam> pageResult = equipmentUpkeepDetailParamService.listPage(EquipmentUpkeepDetailParam.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取保养明细选项列表", description = "根据ID获取保养明细选项列表", method = "GET")
    public Result<EquipmentUpkeepDetailParam> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentUpkeepDetailParam equipmentUpkeepDetailParam = equipmentUpkeepDetailParamService.getById(EquipmentUpkeepDetailParam.class, id);
        return Result.OK(equipmentUpkeepDetailParam);
    }

    @RequestMapping("add")
    @Operation(summary = "保存保养明细选项", description = "保存保养明细选项")
    public Result<EquipmentUpkeepDetailParam> add(@RequestBody EquipmentUpkeepDetailParam equipmentUpkeepDetailParam) {
        equipmentUpkeepDetailParamService.save(equipmentUpkeepDetailParam,new SmartCrudPolicyAdapter(){});
        return Result.OK(equipmentUpkeepDetailParam);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑保养明细选项", description = "编辑保养明细选项", method = "PUT")
    public Result<EquipmentUpkeepDetailParam> edit(@RequestBody EquipmentUpkeepDetailParam equipmentUpkeepDetailParam) {
        equipmentUpkeepDetailParamService.update(equipmentUpkeepDetailParam);
        return Result.OK(equipmentUpkeepDetailParam);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除保养明细选项", description = "删除保养明细选项,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentUpkeepDetailParam> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentUpkeepDetailParamService.delete(EquipmentUpkeepDetailParam.class, key);
        }
        return Result.OK();
    }


}
